package com.gzf.encrption;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.TextView;

import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

public class RSAActivity extends AppCompatActivity {

    private TextView mTextView;
    private PrivateKey mPrivateKey;
    private PublicKey mPublicKey;
    private String mText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rsa);
        initView();
    }

    private void initView() {
        mTextView = (TextView) findViewById(R.id.showinfo);
    }

    //生成公钥私钥
    public void generate(View view) {
        new Thread(new Runnable() {


            @Override
            public void run() {
                try {
                    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
                    keyPairGenerator.initialize(1024);//密码长度bit
                    //获取公钥私钥
                    KeyPair keyPair = keyPairGenerator.generateKeyPair();
                    //私钥
                    mPrivateKey = keyPair.getPrivate();
                    //公钥
                    mPublicKey = keyPair.getPublic();
                    //私钥进行保存，必须进行Base64的转换
                    byte[] mPrivateKeyEncoded = mPrivateKey.getEncoded();
                    byte[] mPublicKeyEncoded = mPublicKey.getEncoded();
                    final byte[] privateKeyBase64Encode = Base64.encode(mPrivateKeyEncoded, Base64.DEFAULT);
                    final byte[] publicKeyBase64Encode = Base64.encode(mPublicKeyEncoded, Base64.DEFAULT);
                    mTextView.post(new Runnable() {
                        @Override
                        public void run() {
                            mTextView.setText("私钥是：" + "\n" + new String(privateKeyBase64Encode) +
                                    "公钥是：\n" + new String(publicKeyBase64Encode));
                        }
                    });
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private String str = "1111111111";

    //使用公钥私钥进行加密解密
    public void encrypt(View view) {
        switch (view.getId()) {
            case R.id.rsa_encrypt:
                try {
                    Cipher cipher = Cipher.getInstance("RSA");
                    if (mPrivateKey != null) {
                        cipher.init(Cipher.ENCRYPT_MODE,mPrivateKey);
                        byte[] encode = cipher.doFinal(str.getBytes());
                        byte[] bytes = Base64.encode(encode, Base64.DEFAULT);
                        mText = new String(bytes);
                        mTextView.setText(mText);
                    }
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.rsa_decrypt:
                try {
                    Cipher cipher = Cipher.getInstance("RSA");
                    if (mPublicKey != null) {
                        cipher.init(Cipher.DECRYPT_MODE,mPublicKey);
                        if (mText != null) {
                            byte[] bytes = Base64.decode(mText.getBytes(), Base64.DEFAULT);
                            byte[] decrypt = cipher.doFinal(bytes);
                            mTextView.setText(new String(decrypt));
                        }
                    }
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}
